<template>
  <div>

    <van-row style="margin-top: 10%; ">
      <van-col span="6">
        <van-cell center>
          <van-image width="50" height="50" src="https://img.yzcdn.cn/vant/cat.jpeg"/>
        </van-cell>
      </van-col>
      <van-col span="6">
        <van-cell center :title="title" :label="label"/>
      </van-col>

    </van-row>


    <van-row type="flex" justify="center" style="margin-top: 10%;">
      <van-col span="18">
        <van-button type="primary" block @click="addFriends">添加好友</van-button>
      </van-col>
    </van-row>

    <van-row type="flex" justify="center" style="margin-top: 10px;">
      <van-col span="18">
        <van-button type="default" block @click="prev">返回上级</van-button>
      </van-col>
    </van-row>

  </div>
</template>

<script>

  import {getRequest} from "network/getRequest";
  import {postRequest} from "network/postRequest";
  import {Dialog} from 'vant';

  export default {
    name: "SearchResult",
    data() {
      return {
        title: '',
        label: '',
        requestId: ''
      }
    },
    created() {
      this.getUserInfo()
    },
    methods: {
      prev() {
        this.$router.go(-1);
      },
      getUserInfo() {
        let phone = this.$route.params.phone;
        let params = {phone}
        getRequest('/user/search', params).then(res => {
          this.label = res.data.phone;
          this.title = res.data.nickName;
          this.requestId = res.data.id;
        })
      },
      addFriends() {

        const data = {
          "sendUserId": JSON.parse(sessionStorage.getItem("userInfo")).id,
          "acceptUserId":this.requestId
        }
        postRequest(data, '/friend/request/add').then(res => {
          if (res.code == 200) {
            Dialog.alert({
              message: '请求发送成功',
            }).then(() => {
              this.$router.push({path: '/login'})
            });
          } else {
            Dialog.alert({
              message: res.msg
            })
          }
        });


      }
    }
  }
</script>

<style scoped>

</style>